package com.dhu.demo.service;

import com.dhu.demo.entity.Student;
import com.dhu.demo.entity.User;
import com.dhu.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class UserService {

    @Autowired(required=false)
    private UserMapper userMapper;

    public List<User> findAll(){return userMapper.findAll();}

    public String insert(User record){
        String account=record.getAccount();
        String msg= "";
        try {
            if(userMapper.judgeByAccount(account).equals("s")||userMapper.judgeByAccount(account).equals("a"))
            {return "用户已存在";}
            else{
                int judge=userMapper.insert(record);
                if(judge==1)
                {
                    return "注册成功";
                }
                else{
                    return "注册失败";
                }
            }
        }
        catch (NullPointerException A)
        {
            int judge=userMapper.insert(record);
            if(judge==1)
            {
                return "注册成功";
            }
            else{
                return "注册失败";
            }
        }
    }

    public int deleteByPrimaryKey(String account){ return userMapper.deleteByPrimaryKey(account); }

    public String judgeLogin(String account,String password){
        String msg = userMapper.selectByAccount(account,password);
        if (msg==null)
        {
            return "查询失败";
        }
        else if(msg.equals("s"))
        {
            return "学生";
        }
        else if(msg.equals("a"))
        {
            return "管理员";
        }
        else {
            return "查询失败";
        }
    }

    public int updatePasswordByAccount(User record){ return userMapper.updatePasswordByAccount(record); }

    public int updateLevelByAccount(User record){ return userMapper.updateLevelByAccount(record); }

    public String getPasswordByAccount(String account){
        return userMapper.getPasswordByAccount(account);
    }
}
